package com.atjava.webspider.model;

import java.util.Date;
import java.util.List;

import com.atjava.jox.annotation.ClassProps;
import com.atjava.jox.annotation.FieldProps;
import com.atjava.jox.annotation.FieldProps.ElementType;
import com.atjava.webspider.consts.Const;
import com.atjava.webspider.logic.Named;
import com.atjava.webspider.thread.TaskListener;
import com.atjava.webspider.util.DateUtil;

@ClassProps(alias="task")
public class Task implements Named{
	
	private String id;
	
	@FieldProps
	private int dataNum;
	
	private TaskListener listener;
	
	@FieldProps
	private Date startTime;
	
	@FieldProps
	private String name;
	
	@FieldProps
	private int state;
	
	@FieldProps
	private String url;
	
	@FieldProps
	private int maxDepth;
	
	@FieldProps
	private String savePath;
	
	@FieldProps
	private String site;
	
	@FieldProps(type=ElementType.ELEMENT)
	private List<Resource> resources;
	
	@FieldProps(type=ElementType.ELEMENT)
	private List<Output> outputs;
	
	private int index = -1;
	
	private StringBuffer urlBuffer = new StringBuffer();
	
	public StringBuffer getUrlBuffer() {
		return urlBuffer;
	}
	
	public int getIndex(){
		return index;
	}
	
	public void setIndex(int index){
		this.index = index;
	}

	public String getPassedTime(){
		if(startTime != null){
			Date now = new Date();
			long l = now.getTime() - startTime.getTime();
			long day = l / (24 * 60 * 60 * 1000);
			long hour = (l / (60 * 60 * 1000) - day * 24);
			long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
			return (hour + "小时" + min + "分" + s + "秒");
		}
		return "";
	}
	
	public List<Output> getOutputs() {
		return outputs;
	}

	public void setOutputs(List<Output> outputs) {
		this.outputs = outputs;
	}

	public String getStateName(){
		return Const.getTaskStateName(state);
	}
	
	public TaskListener getListener() {
		return listener;
	}

	public void setListener(TaskListener listener) {
		this.listener = listener;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public int getMaxDepth() {
		return maxDepth;
	}

	public void setMaxDepth(int maxDepth) {
		this.maxDepth = maxDepth;
	}

	public int getDataNum() {
		return dataNum;
	}

	public synchronized void setDataNum(int dataNum) {
		this.dataNum = dataNum;
	}

	public Date getStartTime() {
		return startTime;
	}
	
	public String getStartTimeStr() {
		if(startTime == null)
			return "";
		return DateUtil.formatTime(startTime);
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}
}
